library(foreign)
library(ggplot2)
library(coefplot)
library(arm)
library(Rmisc)
library(RColorBrewer)
library(readstata13)
library(dplyr)

## Distributions of four types
mydata <-read.dta13("dataset_main.dta")
names(mydata)
mydata[1:5,]

## Figure 
df_table1 <- table(mydata$types_w1)
df1 <- data.frame(cbind(df_table1, prop.table(df_table1)))
df1$Types <- c(1,2,3,4)
colnames(df1) <- c("Frequency", "Percent","Types")
df1$Types_s <- as.character(df1$Types)

p1 = ggplot(df1, aes(x=Types, y=Percent, fill=Types_s)) + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="dodge", stat="identity", width=.7) + xlab("") + scale_x_continuous(labels=c("","","","","")) + ylab("Percentage of respondents") + scale_fill_hue(name="", labels=c("Integrator","Exploiter","Diversifier","Resister")) + ggtitle("") + theme(legend.key=element_rect(colour=NA)) + theme(legend.text=element_text(size=rel(1))) + theme(title=element_text(size=rel(1))) + theme(axis.title.y = element_text(size = rel(1), angle = 90)) + theme(axis.text.y = element_text(size = rel(1))) + theme(axis.title.x = element_text(size = rel(1), angle = 00)) + geom_text(aes(label=sprintf("%0.2f", round(Percent, digits=2),size=1))) + theme_minimal()


## Figure A 
df_table3 <- table(mydata$types_w3)
df3 <- data.frame(cbind(df_table3, prop.table(df_table3)))
df3$Types <- c(1,2,3,4)
colnames(df3) <- c("Frequency", "Percent","Types")
df3$Types_s <- as.character(df3$Types)

p3 = ggplot(df3, aes(x=Types, y=Percent, fill=Types_s)) + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="dodge", stat="identity", width=.7) + xlab("") + scale_x_continuous(labels=c("","","","","")) + ylab("Percentage of respondents") + scale_fill_hue(name="", labels=c("Integrator","Exploiter","Diversifier","Resister")) +
ggtitle("") + theme(legend.key=element_rect(colour=NA)) + theme(legend.text=element_text(size=rel(1))) + theme(title=element_text(size=rel(1))) +
theme(axis.title.y = element_text(size = rel(1), angle = 90)) + theme(axis.text.y = element_text(size = rel(1))) + theme(axis.title.x = element_text(size = rel(1), angle = 00)) + geom_text(aes(label=sprintf("%0.2f", round(Percent, digits=2),size=1))) + theme_minimal()


## Figure B
mydata_rural <-read.dta13("dataset_rural.dta")
names(mydata_rural)
mydata_rural[1:5,]

df_table_rural <- table(mydata_rural$types_w1)
df_rural <- data.frame(cbind(df_table_rural, prop.table(df_table_rural)))
df_rural$Types <- c(1,2,3,4)
colnames(df_rural) <- c("Frequency", "Percent","Types")
df_rural$Types_s <- as.character(df_rural$Types)

p_rural = ggplot(df_rural, aes(x=Types, y=Percent, fill=Types_s)) + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="dodge", stat="identity", width=.7) + xlab("") + scale_x_continuous(labels=c("","","","","")) + ylab("Percentage of respondents") + scale_fill_hue(name="", labels=c("Integrator","Exploiter","Diversifier","Resisters")) +
ggtitle("") + theme(legend.key=element_rect(colour=NA)) + theme(legend.text=element_text(size=rel(1))) + theme(title=element_text(size=rel(1))) +
theme(axis.title.y = element_text(size = rel(1), angle = 90)) + theme(axis.text.y = element_text(size = rel(1))) + theme(axis.title.x = element_text(size = rel(1), angle = 00)) + geom_text(aes(label=sprintf("%0.2f", round(Percent, digits=2),size=1))) + theme_minimal()


## Figure I
mydata_ESS <-read.dta("dataset_ESS.dta")
names(mydata_ESS)
mydata_ESS[1:5,]

mydata_ESS$types_ESS_s <- as.character(mydata_ESS$types)

p_ESS = ggplot(mydata_ESS, aes(x=reorder(cntry_id, percent), y=percent, fill= types_ESS_s)) + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="stack", stat="identity", width=.7) + xlab("") + ylab("Percentage of respondents") + scale_fill_hue(name="", labels=c("Integrator","Exploiter","Diversifier","Resister")) +
ggtitle("") + theme(legend.key=element_rect(colour=NA)) + theme(legend.text=element_text(size=rel(1))) + theme(title=element_text(size=rel(1))) + theme_minimal() +
theme(axis.title.y = element_text(size = rel(1), angle = 90)) + theme(axis.text.y = element_text(size = rel(1))) + theme(axis.text.x = element_text(angle=45, size = rel(1), hjust=1))



## Policy conjoint
library(lmtest)
library(sandwich)
library(msm)
library(cjoint)
library(cregg)
library(foreign)

Policy_conjoint <- read.dta("dataset_conjoint.dta")


## Figure 1
mm_by_type <- cj(Policy_conjoint, chosen ~ Country + Industry + Skill + Gender + Length + Number, id=~IDs, estimate="mm",by=~types_w1, alpha=0.1)

plot(mm_by_type, group="types_w1", vline=0.5, legend_title=NULL,theme=ggplot2:: theme_minimal()) + theme(panel.grid.major=element_line(size=0.25,linetype='solid'),panel.grid.minor=element_line(size=0.25,linetype='solid')) + scale_colour_discrete(breaks=c("1","2","3","4"),labels=c("Integrators","Exploiters","Diversifiers","Resisters"))


## Figure C
mm_by_type_sep <- cj(Policy_conjoint, chosen ~ Country + Industry + Skill + Gender + Length + Number, id=~IDs, estimate="mm",by=~types_w1, alpha=0.1)

plot(mm_by_type_sep, group="types_w1", vline=0.5, legend_title=NULL, theme=ggplot2::theme_minimal()) + ggplot2::facet_wrap(~BY, ncol=2L) + theme(panel.grid.major=element_line(size=0.25,linetype='solid'),panel.grid.minor=element_line(size=0.25,linetype='solid')) + scale_colour_discrete(breaks=c("1","2","3","4"),labels=c("Integrators","Exploiters","Diversifiers","Resisters"))



## Figure D
mm_by_type_diff <- cj(Policy_conjoint, chosen ~ Country + Industry + Skill + Gender + Length + Number, id=~IDs, estimate="mm_differences",by=~types_w1, alpha=0.1)

plot(mm_by_type_diff, group="types_w1", vline=0, legend_title=NULL, theme=ggplot2::theme_minimal()) + theme(panel.grid.major=element_line(size=0.25,linetype='solid'),panel.grid.minor=element_line(size=0.25,linetype='solid')) + scale_colour_discrete(breaks=c("2","3","4"),labels=c("Exploiters","Diversifiers","Resisters"))


## Table G
cj_anova(Policy_conjoint, chosen ~ Industry + Skill + Gender + Length + Number, by=~ Country)
cj_anova(Policy_conjoint, chosen ~ Country + Skill + Gender + Length + Number, by=~ Industry)
cj_anova(Policy_conjoint, chosen ~ Country + Industry + Gender + Length + Number, by=~ Skill)
cj_anova(Policy_conjoint, chosen ~ Country + Industry + Skill + Length + Number, by=~ Gender)
cj_anova(Policy_conjoint, chosen ~ Country + Industry + Skill + Gender + Number, by=~ Length)
cj_anova(Policy_conjoint, chosen ~ Country + Industry + Skill + Gender + Length, by=~ Number)


## Figures E and F
library(foreign)
library(ggplot2)
library(coefplot)
library(arm)
library(Rmisc)
library(RColorBrewer)
library(readstata13)
library(dplyr)

mydata_origin <-read.dta13("dataset_origin.dta")
names(mydata_origin)
mydata_origin[1:5,]

mydata_origin1 <- subset(mydata_origin, (short_who == 1 | short_who == 4) & managers_private == 1)
mydata_origin1_SE <- summarySE(mydata_origin1, measurevar="econ_eval_w1", groupvars=c("short_who"), na.rm=TRUE)
mydata_origin1_SE$short_who <- factor(mydata_origin1_SE$short_who)
mydata_origin1_SE_c <- summarySE(mydata_origin1, measurevar="cul_eval_w1", groupvars=c("short_who"), na.rm=TRUE)
mydata_origin1_SE_c$short_who <- factor(mydata_origin1_SE$short_who)

p_E1 = ggplot(mydata_origin1_SE, aes(x=short_who, y=econ_eval_w1, fill=short_who)) + geom_bar(position=position_dodge(), stat="identity") + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="stack", stat="identity", width=.7) + xlab("") + ylab("Percentage of positive economic evaluations") + scale_fill_hue(name="", labels=c("China (N=32)","SE Asia (N=57)")) + labs(title="Positive evaluations by perceived country of origin", subtitle="Corporate managers") + theme(plot.title=element_text(hjust=0.5), plot.subtitle=element_text(hjust=0.5)) + geom_errorbar(aes(ymin=econ_eval_w1-se,ymax=econ_eval_w1 +se),width=.2,position=position_dodge(.9)) + theme_bw() + theme(axis.text.x = element_blank()) 

p_F1 = ggplot(mydata_origin1_SE_c, aes(x=short_who, y=cul_eval_w1, fill=short_who)) + geom_bar(position=position_dodge(), stat="identity") + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="stack", stat="identity", width=.7) + xlab("") + ylab("Percentage of positive cultural evaluations") + scale_fill_hue(name="", labels=c("China (N=32)","SE Asia (N=57)")) + labs(title="Positive evaluations by perceived country of origin", subtitle="Corporate managers") + theme(plot.title=element_text(hjust=0.5), plot.subtitle=element_text(hjust=0.5)) + geom_errorbar(aes(ymin=cul_eval_w1-se,ymax=cul_eval_w1+se),width=.2,position=position_dodge(.9)) + theme_bw() + theme(axis.text.x = element_blank())  

mydata_origin2 <- subset(mydata_origin, (short_who == 1 | short_who == 4) & RWA_factor >= 0.4790633)
mydata_origin2_SE <- summarySE(mydata_origin2, measurevar="econ_eval_w1", groupvars=c("short_who"), na.rm=TRUE)
mydata_origin2_SE$short_who <- factor(mydata_origin2_SE$short_who)
mydata_origin2_SE_c <- summarySE(mydata_origin2, measurevar="cul_eval_w1", groupvars=c("short_who"), na.rm=TRUE)
mydata_origin2_SE_c$short_who <- factor(mydata_origin2_SE$short_who)

p_E2 = ggplot(mydata_origin2_SE, aes(x=short_who, y=econ_eval_w1, fill=short_who)) + geom_bar(position=position_dodge(), stat="identity") + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="stack", stat="identity", width=.7) + xlab("") + ylab("Percentage of positive economic evaluations") + scale_fill_hue(name="", labels=c("China (N=272)","SE Asia (N=497)")) + labs(title="Positive evaluations by perceived country of origin", subtitle="Right-wing authoritarianism") + theme(plot.title=element_text(hjust=0.5), plot.subtitle=element_text(hjust=0.5)) + geom_errorbar(aes(ymin=econ_eval_w1-se,ymax=econ_eval_w1+se),width=.2,position=position_dodge(.9)) + theme_bw() + theme(axis.text.x = element_blank()) 

p_F2 = ggplot(mydata_origin2_SE_c, aes(x=short_who, y=cul_eval_w1, fill=short_who)) + geom_bar(position=position_dodge(), stat="identity") + coord_cartesian(ylim = c(0, 1)) + geom_bar(position="stack", stat="identity", width=.7) + xlab("") + ylab("Percentage of positive cultural evaluations") + scale_fill_hue(name="", labels=c("China (N=271)","SE Asia (N=506)")) + labs(title="Positive evaluations by perceived country of origin", subtitle="Right-wing authoritarianism") + theme(plot.title=element_text(hjust=0.5), plot.subtitle=element_text(hjust=0.5)) + geom_errorbar(aes(ymin=cul_eval_w1-se,ymax=cul_eval_w1 +se),width=.2,position=position_dodge(.9)) + theme_bw() + theme(axis.text.x = element_blank())  


## Figure G
library(lmtest)
library(sandwich)
library(msm)
library(cjoint)
library(cregg)
library(foreign)

Policy_conjoint <-read.dta("dataset_conjoint.dta")

Policy_conjoint$cul_eval_a[Policy_conjoint$cul_o_eval == 3 | Policy_conjoint$cul_o_eval == 5] <- 0
Policy_conjoint$cul_eval_a[Policy_conjoint$cul_o_eval == 4 | Policy_conjoint$cul_o_eval == 6] <- 1

Policy_conjoint$types_w1_a[Policy_conjoint$cul_eval_a == 1 & Policy_conjoint$econ_eval == 1] <- 1
Policy_conjoint$types_w1_a[Policy_conjoint$cul_eval_a == 0 & Policy_conjoint$econ_eval == 1] <- 2
Policy_conjoint$types_w1_a[Policy_conjoint$cul_eval_a == 1 & Policy_conjoint$econ_eval == 0] <- 3
Policy_conjoint$types_w1_a[Policy_conjoint$cul_eval_a == 0 & Policy_conjoint$econ_eval == 0] <- 4


mm_by_type_a <- cj(Policy_conjoint, chosen ~ Country + Industry + Skill + Gender + Length + Number, id=~IDs, estimate="mm",by=~types_w1_a, alpha=0.1)

plot(mm_by_type_a, group="types_w1_a", vline=0.5, legend_title=NULL,theme=ggplot2:: theme_minimal()) + theme(panel.grid.major=element_line(size=0.25,linetype='solid'),panel.grid.minor=element_line(size=0.25,linetype='solid')) + scale_colour_discrete(breaks=c("1","2","3","4"),labels=c("Integrators","Exploiters","Diversifiers","Resisters"))


## Figure H
library(nFactors)
Policy_conjoint <-read.dta("dataset_conjoint.dta")

factor_mydata <- select(Policy_conjoint, cul_o_eval)

factor_mydata$culture1 <-  0
factor_mydata$culture2 <-  0
factor_mydata$culture3 <-  0
factor_mydata$culture4 <-  0
factor_mydata$culture5 <-  0
factor_mydata$culture6 <-  0

factor_mydata$culture1[Policy_conjoint$cul_o_eval == 1] <-  1
factor_mydata$culture2[Policy_conjoint$cul_o_eval == 2] <-  1
factor_mydata$culture3[Policy_conjoint$cul_o_eval == 3] <-  1
factor_mydata$culture4[Policy_conjoint$cul_o_eval == 4] <-  1
factor_mydata$culture5[Policy_conjoint$cul_o_eval == 5] <-  1
factor_mydata$culture6[Policy_conjoint$cul_o_eval == 6] <-  1

factor_mydata=factor_mydata[complete.cases(factor_mydata),]
factor_mydata1 <- select(factor_mydata, culture1:culture6)

ev <- eigen(cor(factor_mydata1))
ap <- parallel(subject=nrow(factor_mydata1), var=ncol(factor_mydata1),rep=100,cent=.05)
nS <- nScree(x=ev$values,aparallel-ap$eigen$gevpea)
plotnScree(nS)



## Multinomial logit
library(foreign)
library(ggplot2)
library(coefplot)
library(arm)
library(Rmisc)
library(RColorBrewer)
library(clusterSim)
library(nnet) 
library(boot) 
library(effects)
library(simcf) 
library(stargazer)
library(reshape2)

mydata <-read.dta13("dataset_main.dta")
mydata$types<-NULL

mydata$types[mydata$types_w1==1]<-"1.Integrator"
mydata$types[mydata$types_w1==2]<-"2.Exploiter"
mydata$types[mydata$types_w1==3]<-"3.Diversifier"
mydata$types[mydata$types_w1==4]<-"4.Discriminator"

table(mydata$types)

# change the ref category to 4
mydata$types2 <- as.factor(mydata$types)
mydata$types2 <- relevel(mydata$types2,ref="4.Discriminator")

## Tables 3 and H
main <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + for_ami_friend_d + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata, model=TRUE) 
stargazer(main, t.auto=FALSE, p.auto=FALSE)
stargazer(main, apply.coef=exp, apply.se=exp, t.auto=FALSE, p.auto=FALSE)

## Table I 
main_mun <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + mun_foreign + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata,  model=TRUE) 
stargazer(main_mun, t.auto=FALSE, p.auto=FALSE)


## Table J
main_mun_change5_15 <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + mun_change5_15 + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata, model=TRUE) 
stargazer(main_mun_change5_15, t.auto=FALSE, p.auto=FALSE)


## Table K
main_mun_change10_15 <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + mun_change10_15 + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata,model=TRUE) 
stargazer(main_mun_change10_15, t.auto=FALSE, p.auto=FALSE)


## Table L
main_mun_alt <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + mun_foreign + for_ami_friend_d + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata,  model=TRUE) 
stargazer(main_mun_alt, t.auto=FALSE, p.auto=FALSE)


## Table M
main_mun_change5_15_alt <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + mun_change5_15 + for_ami_friend_d + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata, model=TRUE) 
stargazer(main_mun_change5_15_alt, t.auto=FALSE, p.auto=FALSE)


## Table N
main_mun_change10_15_alt <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + mun_change10_15 + for_ami_friend_d + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata,model=TRUE) 
stargazer(main_mun_change10_15_alt, t.auto=FALSE, p.auto=FALSE)


## Table O
main_worker <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + for_ami_worker_d + RWA_factor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata,  model=TRUE) 
stargazer(main_worker, t.auto=FALSE, p.auto=FALSE)


## Table P
mydata$admin_job[mydata$admin == 3] <- "Admin"
mydata$admin_job[mydata$admin !=3] <- "No admin"
mydata$admin_job <- factor(mydata$admin_job)

main_admin <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + for_ami_friend_d + RWA_factor +  managers_private + restaurant_hotel_worker + admin_job, Hess=TRUE, na.rm=T, data=mydata, model=TRUE) 
stargazer(main_admin, t.auto=FALSE, p.auto=FALSE)


## Table Q 
main_ethno <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + for_ami_friend_d + ethno_nor +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata, model=TRUE) 
stargazer(main_ethno, t.auto=FALSE, p.auto=FALSE)


## Table R 
main_without_RWA <- multinom(types2~male + age_normal + college_above + income_normal + unemployment_real + LDP  + for_ami_friend_d +  managers_private + restaurant_hotel_worker, Hess=TRUE, na.rm=T, data=mydata, model=TRUE) 
stargazer(main_without_RWA, t.auto=FALSE, p.auto=FALSE)
